import Fs from "fs-extra";
import Path from "path";
import { exec } from "./exec.mjs";
// eslint-disable-next-line no-unused-vars
import klawSync from "klaw-sync";
// eslint-disable-next-line no-unused-vars
import Esbuild from "esbuild";
(async () => {
  Fs.removeSync(Path.resolve("dist/vue"));
  await exec("npx nuxt build");
  Fs.copySync(Path.resolve("vue/.output"), Path.resolve("dist/vue"), {
    overwrite: true,
  });
  klawSync(Path.resolve("dist/vue"), { nodir: true }).forEach((e) => {
    let toPath = e.path.replace(/\.mjs$/, ".js");
    /.mjs$/.test(e.path) &&
      Esbuild.build({
        entryPoints: [e.path],
        platform: "node",
        bundle: true,
        outfile: toPath,
        allowOverwrite: true,
        format: "cjs",
      })
        .then(() => {
          let content = Fs.readFileSync(toPath, "utf8");
          Fs.writeFileSync(
            toPath.replace(/\.mjs"/g, `.js"`).replace(/\.mjs'/g, ".js'"),
            content
          );
          // return Fs.removeSync(e.path);
          return 1;
        })
        .catch(console.error);
  });
})();
